package com.haian.slidealbum.model.helper;

import java.io.IOException;

import android.content.Context;
import android.database.Cursor;

public class DBHandler {

	//_________________ DB
	public static void initdb(Context context) {
		DBHelper mDBHelper = new DBHelper(context);
		try {
			mDBHelper.createDataBase();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//_______________________ Interval
	public static int getInterval(Context context)
	{
		int interval = 5;
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursorItem = mDBHelper.getInterval();
		if(cursorItem.moveToFirst())
		{
			interval = Integer.parseInt(cursorItem.getString(0));
		}
		mDBHelper.closeDB();
		return interval;
	}
	
	public static void updateInterval(Context context, String strInterval)
	{
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.updateInterval(strInterval);
	}
	
	//______________________Album
	public static boolean insertAlbum(Context context, String nameAlbum, String currentAlbum)
	{
		DBHelper mDBHelper = new DBHelper(context);
		boolean checkInsert = mDBHelper.insertAlbum(nameAlbum, currentAlbum);
		mDBHelper.closeDB();
		return checkInsert;
	}
	
	public static void disableCurrentAlbum(Context context)
	{
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.disableCurrentAlbum();
	}
	
	public static Cursor getAllAlbum(Context context) {
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getAllAlbum();
		return cursor;
	}
	
	public static int getMaxIdAlbum(Context context) {
		int idAlbum = 0;
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getAllAlbum();
		if(cursor.moveToLast())
		{
			idAlbum = cursor.getInt(0);
		}
		return idAlbum;
	}
	
	public static Cursor getAlbumById(Context context, int id) {
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getAlbumById(id);
		return cursor;
	}
	
	public static Cursor getAlbumCurrent(Context context) {
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getAlbumCurrent();
		return cursor;
	}
	
	public static void deleteAlbum(Context context,int id) {
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.deleteAlbum(id);
	}
	
	public static void updateAlbum(Context context, int id, String nameAlbum, String currentAlbum) {
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.updateAlbum(id, nameAlbum, currentAlbum);
	}
	
	//______________________Item
	
	public static boolean insertItem(Context context, String urlImage, String idAlbum, String itemCover) {
		DBHelper mDBHelper = new DBHelper(context);
		boolean checkInsert = mDBHelper.insertItem(urlImage, idAlbum, itemCover);
		mDBHelper.closeDB();
		return checkInsert;
	}
	
	public static Cursor getItemCoverByAlbumId(Context context, String albumid) {
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getItemCoverByAlbumId(albumid);
		return cursor;
	}
	
	public static Cursor getItemByAlbumId(Context context, String albumid) {
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getItemByAlbumId(albumid);
		return cursor;
	}
	
	public static void deleteItemByAlbumId(Context context, String albumid) {
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.deleteItemByAlbumId(albumid);
	}
	
	public static void deleteItemById(Context context, int id) {
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.deleteItemById(id);
	}

	public static  Cursor getItemById(Context context, int id)
	{
		DBHelper mDBHelper = new DBHelper(context);
		Cursor cursor = mDBHelper.getItemById(id);
		return cursor;
	}
	
	public static void updateItemCover(Context context, int id, String idAlbum, String itemCover) {
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.updateItemCover(id, idAlbum, itemCover);
	}
	
	public static void closeDB(Context context)
	{
		DBHelper mDBHelper = new DBHelper(context);
		mDBHelper.closeDB();
	}
}
